home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / More Sprocket Examples 1.0 / DroneZone Sources / DZEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-12  |  5.9 KB  |  307 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        DZEvents.c
  3.  *
  4.  *    Contents:    Handles the events.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #include <assert.h>
  10.  
  11. #include <AppleEvents.h>
  12. #include <Events.h>
  13. #include <ToolUtils.h>
  14. #include <Types.h>
  15.  
  16. #include "DZDisplay.h"
  17. #include "DZEvents.h"
  18. #include "DZGame.h"
  19. #include "DZMain.h"
  20. #include "DZMenu.h"
  21.  
  22.  
  23. static void Events_MouseDown(
  24.     const EventRecord*        inEvent);
  25.  
  26. static void Events_KeyDown(
  27.     const EventRecord*        inEvent);
  28.  
  29. static void Events_AutoKey(
  30.     const EventRecord*        inEvent);
  31.  
  32. static void Events_Update(
  33.     const EventRecord*        inEvent);
  34.  
  35. static void Events_Activate(
  36.     const EventRecord*        inEvent);
  37.  
  38. static void Events_OS(
  39.     const EventRecord*        inEvent);
  40.  
  41. static void Events_HighLevel(
  42.     const EventRecord*        inEvent);
  43.  
  44.  
  45. /* =============================================================================
  46.  *        Events_Init (external)
  47.  *
  48.  *    Initializes the events stuff.
  49.  * ========================================================================== */
  50. void Events_Init(
  51.     void)
  52. {
  53.     FlushEvents(everyEvent, 0);
  54. }
  55.  
  56.  
  57. /* =============================================================================
  58.  *        Events_Exit (external)
  59.  *
  60.  *    Prepares for exit.
  61.  * ========================================================================== */
  62. void Events_Exit(
  63.     void)
  64. {
  65. }
  66.  
  67.  
  68. /* =============================================================================
  69.  *        Events_Process (external)
  70.  *
  71.  *    Processes any pending events.
  72.  * ========================================================================== */
  73. void Events_Process(
  74.     void)
  75. {
  76.     EventRecord                ev;
  77.     
  78.     while (GetNextEvent(everyEvent, &ev))
  79.     {
  80.         switch (ev.what)
  81.         {
  82.             case mouseDown:
  83.                 Events_MouseDown(&ev);
  84.             break;
  85.             
  86.             case keyDown:
  87.                 Events_KeyDown(&ev);
  88.             break;
  89.             
  90.             case autoKey:
  91.                 Events_AutoKey(&ev);
  92.             break;
  93.             
  94.             case updateEvt:
  95.                 Events_Update(&ev);
  96.             break;
  97.             
  98.             case activateEvt:
  99.                 Events_Activate(&ev);
  100.             break;
  101.             
  102.             case osEvt:
  103.                 Events_OS(&ev);
  104.             break;
  105.             
  106.             case kHighLevelEvent:
  107.                 Events_HighLevel(&ev);
  108.             break;
  109.             
  110.         }
  111.     }
  112.     
  113.     SystemTask();
  114. }
  115.  
  116.  
  117. /* =============================================================================
  118.  *        Events_MouseDown (internal)
  119.  *
  120.  *    Processes a mouse-down event.
  121.  * ========================================================================== */
  122. void Events_MouseDown(
  123.     const EventRecord*        inEvent)
  124. {
  125.     short                     part;
  126.     WindowPtr                wind;
  127.     long                    menu;
  128.     Rect                    limits;
  129.     long                    size;
  130.     
  131.     assert(inEvent != NULL);
  132.     
  133.     part = FindWindow(inEvent->where, &wind);
  134.     
  135.     switch (part)
  136.     {
  137.         case inMenuBar:
  138.             menu = MenuSelect(inEvent->where);
  139.             Menu_Select(HiWord(menu), LoWord(menu));
  140.         break;
  141.             
  142.         case inSysWindow:
  143.             SystemClick(inEvent, wind);
  144.         break;
  145.             
  146.         case inDrag:
  147.             DragWindow(wind, inEvent->where, &qd.screenBits.bounds);
  148.         break;
  149.             
  150.         case inGoAway:
  151.             Main_LastRoundup();
  152.         break;
  153.                 
  154.         case inContent:
  155.             // nothing yet...
  156.         break;
  157.             
  158.         case inGrow:
  159.             limits.top    = 64;
  160.             limits.left   = 64;
  161.             limits.bottom = 4096;
  162.             limits.right  = 4096;
  163.             
  164.             size = GrowWindow(wind, inEvent->where, &limits);
  165.             
  166.             if (size != 0)
  167.             {
  168.                 SetPort(wind);
  169.                 EraseRect(&wind->portRect);
  170.                 SizeWindow(wind, LoWord(size), HiWord(size), true);
  171.                 InvalRect(&wind->portRect);
  172.                 
  173.                 if (wind == Display_GetWindow())
  174.                 {
  175.                     Display_Resize();
  176.                 }
  177.             } 
  178.         break;
  179.     }
  180. }
  181.  
  182.  
  183. /* =============================================================================
  184.  *        Events_KeyDown (internal)
  185.  *
  186.  *    Processes a key-down event.
  187.  * ========================================================================== */
  188. void Events_KeyDown(
  189.     const EventRecord*        inEvent)
  190. {
  191.     unsigned long            ch;
  192.     unsigned long            cap;
  193.     long                    menu;
  194.     
  195.     assert(inEvent != NULL);
  196.     
  197.     ch = inEvent->message & charCodeMask;
  198.     cap = (inEvent->message & keyCodeMask) >> 8;
  199.     
  200.     if ((inEvent->modifiers & cmdKey) != 0)
  201.     {
  202.         menu = MenuKey(ch);
  203.         Menu_Select(HiWord(menu), LoWord(menu));
  204.     }
  205.     else if (ch == 0x1b)
  206.     {
  207.         if (Game_GetState() == kGameState_Paused)
  208.         {
  209.             Game_SetState(kGameState_Playing);
  210.         }
  211.     }
  212. }
  213.  
  214.  
  215. /* =============================================================================
  216.  *        Events_AutoKey (internal)
  217.  *
  218.  *    Processes an auto-key event.
  219.  * ========================================================================== */
  220. void Events_AutoKey(
  221.     const EventRecord*        inEvent)
  222. {
  223.     // Do nothing for now
  224. }
  225.  
  226.  
  227. /* =============================================================================
  228.  *        Events_Update (internal)
  229.  *
  230.  *    Processes an update event.
  231.  * ========================================================================== */
  232. void Events_Update(
  233.     const EventRecord*        inEvent)
  234. {
  235.     WindowPtr                wind;
  236.     
  237.     assert(inEvent != NULL);
  238.     
  239.     wind = (WindowPtr) inEvent->message;
  240.     
  241.     BeginUpdate(wind);
  242.     
  243.     if (wind == Display_GetWindow())
  244.     {
  245.         Display_DrawGrow();
  246.         Display_DrawContents();
  247.     }
  248.     
  249.     EndUpdate(wind);
  250. }
  251.  
  252.  
  253. /* =============================================================================
  254.  *        Events_Activate (internal)
  255.  *
  256.  *    Processes an activate event.
  257.  * ========================================================================== */
  258. void Events_Activate(
  259.     const EventRecord*        inEvent)
  260. {
  261.     WindowPtr                wind;
  262.     
  263.     assert(inEvent != NULL);
  264.     
  265.     wind = (WindowPtr) inEvent->message;
  266.     
  267.     if (wind == Display_GetWindow())
  268.     {
  269.         Display_Activate((inEvent->modifiers & activeFlag) != 0);
  270.     }
  271. }
  272.  
  273.  
  274. /* =============================================================================
  275.  *        Events_OS (internal)
  276.  *
  277.  *    Processes an OS event.
  278.  * ========================================================================== */
  279. void Events_OS(
  280.     const EventRecord*        inEvent)
  281. {
  282.     assert(inEvent != NULL);
  283.     
  284.     if ((inEvent->message >> 24) & 0xFF == suspendResumeMessage)
  285.     {
  286.         Display_Activate((inEvent->message & resumeFlag) != 0);
  287.     }
  288. }
  289.  
  290.  
  291. /* =============================================================================
  292.  *        Events_HighLevel (internal)
  293.  *
  294.  *    Processes a high-level event.
  295.  * ========================================================================== */
  296. void Events_HighLevel(
  297.     const EventRecord*        inEvent)
  298. {
  299.     assert(inEvent != NULL);
  300.     
  301.     AEProcessAppleEvent(inEvent);
  302. }
  303.  
  304.  
  305.  
  306.  
  307.